<?php

namespace App\Http\Controllers\Admin\Notice;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class NoticeManagerController extends BaseController {

    /**
     * @api {post} /notice 公告创建
     * @apiVersion 0.1.0
     * @apiName 公告-公告创建
     * @apiGroup 公告
     *
     *
     * @apiParam {string} notice_title                   标题
     * @apiParam {string} notice_content                 内容
     * @apiParam {number} type                              消息类型 1文字  2 图片
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        try {
            $this->validate($request, [
                'notice_title' => 'required|string',
                'notice_content' => 'required|string',
                'type' => 'required|numeric|integer|in:1,2,3',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $params = $request->input();

        $res = app('repo_notice');
        $params['add_time'] = time();
        $params['update_time'] = time();
        $params['admin_id'] = $request->admin_id;

        $rows = $res->addNotice($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /notice 公告删除
     * @apiVersion 0.1.0
     * @apiName 公告-公告删除
     * @apiGroup 公告
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_notice')->delNotice($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /notice/release 公告发布
     * @apiVersion 0.1.0
     * @apiName 公告-公告发布
     * @apiGroup 公告
     *
     * @apiParam {string} id                       公告id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function release(Request $request) {

        try {
            $this->validate($request, [
                'id' => 'required|integer',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }
        $id = $request->input('id');
        $rows = app('repo_notice')->releaseNotice([$id]);
        if (!$rows) {
            throw new BasicException(10001, "发布失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /notice 公告修改
     * @apiVersion 0.1.0
     * @apiName 公告-公告修改
     * @apiGroup 公告
     *
     *
     * @apiParam {string} id
     * @apiParam {string} notice_title                   标题
     * @apiParam {string} notice_content                 内容
     * @apiParam {number} type                              消息类型 1文字  2 图片
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'id' => 'required|integer',
                'notice_title' => 'required|string',
                'notice_content' => 'required|string',
                'type' => 'required|numeric|integer|in:1,2,3',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $id = $request->input('id');
        $res = app('repo_notice');

        $params = $request->input();
        $params['update_time'] = time();
        $params['admin_id'] = $request->admin_id;
        
        $rows = $res->saveNotice(["notice_id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /notice 公告列表
     * @apiVersion 0.1.0
     * @apiName 公告-公告列表
     * @apiGroup 公告
     *
     * @apiParam {string} keyword 关键字
     * @apiParam {number} type 公告类型：1工会 2厅 3全部 可选
     * @apiParam {number} status 状态 数据状态:1草稿箱 2 发出  3 删除(伪删除) 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "notice_id(序号)": 11,
                "notice_title(标题)": "大概大概多少",
                "notice_content(内容)": "地方法规的是法国士大夫敢死队风格是",
                "admin_id(管理员id)": 1,
                "type(公告类型)": 3,
                "status(公告状态)": 3,
                "add_time(添加时间)": "2020",
                "update_time(修改时间)": "2020",
                "admin": {
                    "admin_id": 1,
                    "admin_name(管理员名称)": "admin"
                }
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'keyword' => 'filled|string',
                'type' => 'filled|numeric|in:1,2,3',
                'status' => 'filled|numeric|in:1,2,3',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_notice')->getNoticeQueryList($request->input());

        return Response::sendList($list);
    }


}
